package br.com.unibratec.negocio;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import br.com.unibratec.R;
import br.com.unibratec.util.DataUtil;

public class EventoAdapter extends BaseAdapter {

    private Context context;
    private List<Evento> eventos;

    public EventoAdapter(Context context, List<Evento> eventos) {
        this.context = context;
        this.eventos = eventos;
    }

    @Override
    public int getCount() {
        return this.eventos.size();
    }

    @Override
    public Object getItem(int posicao) {
        return this.eventos.get(posicao);
    }

    @Override
    public long getItemId(int posicao) {
        return this.eventos.get(posicao).getId();
    }

    @Override
    public View getView(int posicao, View view, ViewGroup viewGroup) {
        Evento evento = (Evento) this.getItem(posicao);
        View v = LayoutInflater.from(this.context)
                .inflate(R.layout.lista, null);
        TextView nome = (TextView) v
                .findViewById(R.idListEvento.textViewEvento);
        nome.setText(evento.getDescricao());

        TextView data = (TextView) v
                .findViewById(R.idListEvento.textViewDataHora);
        data.setText(DataUtil.dateToString(evento.getData(), "dd/MM/yyyy HH:mm"));

        try {
            InputStream inputStream = this.context.getAssets().open(
                    evento.getAtivo() + ".png");
            Drawable drawable = Drawable.createFromStream(inputStream, null);
            drawable.setBounds(0, 0, 32, 32);
            nome.setCompoundDrawables(null, null, drawable, null);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return v;
    }
}
